package com.nowcoder.community.controller;

import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Message;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.MessageService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController implements CommunityConstant {
    @Autowired
    MessageService messageService;
    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;
    @Autowired
    HostHolder hostHolder;

    @Autowired
    @Qualifier("thymeleafViewResolver")
    private ThymeleafViewResolver thymeleafViewResolver;

//    查询所有帖子，根据帖子的用户id 查询用户   最后放入到 map中
    @GetMapping("/index")
    public String getIndexPage(Model model, Page page) {
        System.out.println("------------------------------------index执行------------------------------------");
        // 方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model.
        // 所以,在thymeleaf中可以直接访问Page对象中的数据.
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index");

        List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (list != null) {
            for (DiscussPost post : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                User user = userService.findUserById(post.getUserId());
                map.put("user", user);

                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);
                System.out.println("--------------------------------"+user+"--------------------------------");
                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        System.out.println(discussPosts );
        //求未读消息总数
        // 会话列表
        User user=hostHolder.getUser();
        List<Message> conversationList = messageService.findConversations(
                user.getId(),0, Integer.MAX_VALUE);
        int unReadCountSum=0;
        Map<String,Integer> unread=new HashMap<>();
        if (conversationList != null) {
            for (Message message : conversationList) {
                unReadCountSum+=messageService.findLetterUnreadCount(user.getId(), message.getConversationId());
            }
            unread.put("unReadCount",unReadCountSum);
            thymeleafViewResolver.setStaticVariables(unread);
        }


        return "/index";
    }

    @RequestMapping(path = "/error", method = RequestMethod.GET)
    public String getErrorPage() {
        return "/error/500";
    }

}
